package com.xinruke.hostels.manage.user.controller;

import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.controller.BaseController;
import com.xinruke.hostels.common.po.XtJuese;
import com.xinruke.hostels.common.service.OperateLogService;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.manage.user.interfaces.dto.*;
import com.xinruke.hostels.manage.user.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @ClassName RoleController
 * @CreateDate 2019/6/13
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 角色管理
 */
@RestController
@RequestMapping("role")
@Api(tags = "角色管理")
public class RoleController extends BaseController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RoleService roleService;
    @Resource
    OperateLogService operateLogService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加角色")
    public ResultVO addUserInfo(@Valid @RequestBody RoleAddDTO roleAddDTO) {
        ResultVO resultVO = ResultVO.fail();
        roleAddDTO.setJiudianId(SysDicConstant.SUOSHUJIUDIAN);
        roleAddDTO.setSuoshuxitong(XtJuese.SUOSHUXITONG_ZHONGYANG);

        int n = roleService.add(roleAddDTO);
        if (n > 0) {
            operateLogService.writeOperateLog("角色管理", "添加角色", "名称[" + roleAddDTO.getMingcheng() + "]");
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }

    @PostMapping(value = "/modify")
    @ApiOperation(value = "修改角色")
    public ResultVO modify(@Valid @RequestBody RoleModifyDTO roleModifyDTO) {
        ResultVO resultVO = ResultVO.fail();
        int n = roleService.modify(roleModifyDTO);
        if (n > 0) {
            operateLogService.writeOperateLog("角色管理", "修改角色", "id[" + roleModifyDTO.getId() + "]");
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        ;
        return resultVO;
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除角色")
    public ResultVO delete(@Valid @RequestBody RoleDeleteDTO roleDeleteDTO) {
        ResultVO resultVO = ResultVO.fail();
        int n = roleService.delete(roleDeleteDTO);
        if (n > 0) {
            operateLogService.writeOperateLog("角色管理", "删除角色", "id[" + roleDeleteDTO.getId() + "]");
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }

    @PostMapping(value = "/detail")
    @ApiOperation(value = "详情")
    public ResultVO<RoleDetailResultDTO> detail(@Valid @RequestBody RoleDetailQueryDTO roleDeleteDTO) {
        ResultVO<RoleDetailResultDTO> resultVO = ResultVO.fail();
        RoleDetailResultDTO dto = roleService.detail(roleDeleteDTO);

        if (dto != null) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
            resultVO.setData(dto);
        }

        return resultVO;
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "查询角色列表和使用数量")
    public ResultVO<RowsDataVO<RoleQueryResultDTO>> getRoleList(@Valid @RequestBody RoleQueryDTO roleQueryDTO) {
        ResultVO<RowsDataVO<RoleQueryResultDTO>> resultVO = ResultVO.fail();
        //中央使用所有角色
        roleQueryDTO.setJiudianId(SysDicConstant.SUOSHUJIUDIAN);

        RowsDataVO<RoleQueryResultDTO> data = roleService.getRoleList(roleQueryDTO);
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(data);

        return resultVO;
    }
}
